var conn = new net.nextdb.Connection({
  companyName:"games",
  databaseName:"mysticquest"
  });




var mq = {
  init: function(){
    
    mq.setCaptcha();
    
    /** bind elements **/
    
    $("#submit-reset").click(function(){
      $("#mq-reg-form")[0].reset();
    })
    
    $("#submit-request").click(mq.insert);
    
  },
  setCaptcha: function(){
    net.nextdb.Util.sourceCaptchaImg($('#captchaimg').get(0));
  },
  insert: function(){
    if(!mq.validate()){
      return;
    }
    var insert = new net.nextdb.Insert("USER");
    
    insert.setParameters({
      first_name: $("#fname").val(),
      last_name: $("#lname").val(),
      login: $("#login").val(),
      password: $("#password").val(),
      email: $("#email").val(),
      captcha: $("#captcha").val(),
      creation_date: "now"
    });
    
    conn.executeInsert(insert,function(key,error){
      if(error){
        if(error.code == 120){
          mq.showErr("field" + error.causedBy + " must be unique. The value you entered is already taken.");
        }else{
          mq.showErr(error.message);
        }
      } else {
        $("#mq-reg-form").css("display","none");
        mq.showErr("You have successfully created a new user! ")
      }
    });
    
  },
  showErr: function(msg){
    $("#mq-errmsg").html(msg);
  },
  validate: function(){
    return true;
  }
}


$(document).ready(function(){
  mq.init();
})
